package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.GamePageantMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

public class PageantRecycleStrategy extends AbstractItemRecycleStrategy {
    @Override
    public void markNeedRecycleItem() {
        ActivityInfo activityInfo = GamePageantMgr.getActivityInfo();
        if (activityInfo != null) {
            List<Integer> paramList = StringUtils.stringToIntegerList(activityInfo.getParam6(), "\\|");
            long recycleTime = activityInfo.getEndTime() * 1000 + 600 * 1000;
            List<String> arrayList = new ArrayList<>();
            arrayList.add(String.valueOf(paramList.get(0)));
            arrayList.add(activityInfo.getActivityName());
            ActivityItemRecycleMgr.addRecycleItemData(paramList.get(1), activityType, 0, recycleTime, new ArrayList<>(arrayList));
            ActivityItemRecycleMgr.addRecycleItemData(paramList.get(2), activityType, 0, recycleTime, new ArrayList<>(arrayList));
            ActivityItemRecycleMgr.addRecycleItemData(paramList.get(3), activityType, 0, recycleTime, new ArrayList<>(arrayList));
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.PageantActivity.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
        removePageantActivityBagItem(userId, data);
    }

    public void removePageantActivityBagItem(long userId, ActivityItemRecycleData data) {
        Property reward;
        long count;
        GoodsInfo goodsInfo;
        int removeGoodsId = data.getItemId();
        UserBagItem userBagItem = getUserBagItem(userId, removeGoodsId);
        if (null == userBagItem || userBagItem.getNum() <= 0) {
            return;
        }
        count = userBagItem.getNum();

        goodsInfo = GoodsMgr.getGoodsById(removeGoodsId);
        if (null == goodsInfo) {
            log.error("remove pageant activity goods to score error,goods no found,goodsId:{},remove count:{}", removeGoodsId, count);
            return;
        }
        BigInteger score = goodsInfo.getParamList().get(2);       //参数3为转换积分
        if (score.compareTo(BigInteger.ZERO) <= 0) {
            log.error("remove pageant activity goods to score error,goodsId:{},remove count:{}", removeGoodsId, count);
            return;
        }
        score = score.multiply(BigInteger.valueOf(count));
        List<String> paramList = data.getParamList();
        int scoreGoodsId = Integer.parseInt(paramList.get(0));
        String activityName = paramList.get(1);
        reward = new Property(scoreGoodsId, score);
        String rewardStr = PropertyHelper.parsePropertyToString(reward);
        String language = UserMgr.getUserInfo(userId).getLanguage();
        if (!StringUtils.isNullOrEmpty(rewardStr) && removeItem(userBagItem)) {//有将道具转换
            String goodsName = MultipleLanguageMgr.getContent(goodsInfo.getGoodsName(), language);
            activityName = ServerLanguageMgr.getContent(activityName, language);
            String mailTitle = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.PAGEANT_ACTIVITY_GOODS_AUTO_CONSUME_TITLE,
                    language), activityName);
            String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.PAGEANT_ACTIVITY_GOODS_AUTO_CONSUME_CONTENT,
                    language), activityName, goodsName, count);
            MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), rewardStr, mailContent, mailTitle);
        } else {
            if (count > 0) {
                log.error("remove pageant activity goods to score reward is empty,consume goodsId:{},count:{}", removeGoodsId, count);
            }
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
